package com.android.vtca.sudoky;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

import com.android.vtca.lib.game2d.Game2D;
import com.android.vtca.lib.game2d.MySurfaceView;
import com.android.vtca.lib.game2d.Sprite;

public class GamePlay extends Activity implements Game2D, OnClickListener {

	private final static int GAME_MENU = 0;
	private final static int GAME_WIN = GAME_MENU + 1;
	private final static int GAME_LOSE = GAME_WIN + 1;

	private float fWidth, fHeight;
	private Paint paintForeground, paintForegroundLock, paintTimes,
			paintTimesRound, paintSelected;
	private int iPanelWidth, iPanelHeight, iPanelBegin, iPanelEnd, iSelX,
			iSelY;
	private Sprite spHome, spBackGround;
	private Rect recSel;

	private Game mGame;

	private Dialog dlgInGameMenu, dlgLose, dlgWin;
	private EditText editText_WinName;

	private String tvWinContent;
	
	private boolean IsInit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(new MySurfaceView(this, null));
		
		dlgInGameMenu = new Dialog(this);
		dlgInGameMenu.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dlgInGameMenu.setContentView(R.layout.layout_in_game_menu_diglog);

		dlgLose = new Dialog(this);
		dlgLose.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dlgLose.setContentView(R.layout.layout_lose_diglog);

		dlgWin = new Dialog(this);
		dlgWin.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dlgWin.setContentView(R.layout.layout_win_diglog);
		
		IsInit = false;
	}

	public GamePlay() {
		// TODO Auto-generated constructor stub
	}
	
	private void Init (Canvas canvas) {
		iPanelWidth = iPanelHeight = canvas.getWidth();
		iPanelBegin = (canvas.getHeight() - iPanelHeight) / 2;
		iPanelEnd = iPanelBegin + iPanelHeight;
		fWidth = iPanelWidth / 9f;
		fHeight = iPanelHeight / 9f;		

		paintTimes = new Paint();
		paintTimes.setColor(Color.GREEN);

		spHome = new Sprite(this, R.drawable.home);

		spBackGround = new Sprite(this, R.drawable.backgound);
		spBackGround.setPisition(0, 0);

		paintTimesRound = new Paint();
		paintTimesRound.setColor(Color.BLUE);
		paintTimesRound.setStyle(Style.STROKE);

		paintForeground = new Paint(Paint.ANTI_ALIAS_FLAG);
		paintForeground.setColor(getResources().getColor(
				R.color.puzzle_foreground));
		paintForeground.setStyle(Style.FILL);
		paintForeground.setTextAlign(Align.CENTER);

		paintForegroundLock = new Paint(Paint.ANTI_ALIAS_FLAG);
		paintForegroundLock.setColor(getResources().getColor(
				R.color.puzzle_foreground_lock));
		paintForegroundLock.setStyle(Style.FILL);
		paintForegroundLock.setTextAlign(Align.CENTER);

		paintForeground.setTextSize(fHeight * 0.75f);
		paintForeground.setTextScaleX(fWidth / fHeight);

		paintForegroundLock.setTextSize(fHeight * 0.75f);
		paintForegroundLock.setTextScaleX(fWidth / fHeight);

		paintSelected = new Paint();
		paintSelected
				.setColor(getResources().getColor(R.color.puzzle_selected));

		recSel = new Rect();

		mGame = new Game(GameSettings.GetInstance().getDifficultSetting());

		editText_WinName = (EditText) findViewById(R.id.editText_WinName);

		((Button) dlgInGameMenu.findViewById(R.id.btn_IGM_mainmenu))
				.setOnClickListener(this);
		((Button) dlgInGameMenu.findViewById(R.id.btn_IGM_Restart))
				.setOnClickListener(this);
		((Button) dlgInGameMenu.findViewById(R.id.btn_IGM_Resume))
				.setOnClickListener(this);

		((Button) dlgLose.findViewById(R.id.btn_Lose_MainMenu))
				.setOnClickListener(this);
		((Button) dlgLose.findViewById(R.id.btn_LoseRestart))
				.setOnClickListener(this);

		((Button) dlgWin.findViewById(R.id.btn_WinOK)).setOnClickListener(this);
		
		spHome.setPisition((canvas.getWidth() - spHome.getWidth()) / 2,
				canvas.getHeight() - spHome.getHeight());
		
		paintForeground.setTextSize(fHeight * 0.75f);
		paintForeground.setTextScaleX(fWidth / fHeight);
		
		IsInit = true;
	}

	@Override
	public void Draw(Canvas canvas) {
		if(!IsInit) {
			Init(canvas);
		}		
		
		DrawBackGround(canvas);
		DrawTitle(canvas);
		DrawTimes(canvas, 100);
		DrawPanel(canvas);
		spHome.DrawIamge(canvas);
		DrawGame(canvas);
		DrawSelected(canvas);

	}

	@Override
	public void Update() {
		// TODO Auto-generated method stub

	}

	@Override
	public void Touch(MotionEvent event) {
		if (event.getY() >= iPanelBegin && event.getY() <= iPanelEnd) {
			selected((int) (event.getX() / fWidth),
					(int) ((event.getY() - iPanelBegin) / fHeight));
			ShowKeypad(iSelX, iSelY);
		}

	}

	public void DrawBackGround(Canvas canvas) {
		spBackGround.DrawIamge(canvas);
	}

	public void DrawTitle(Canvas canvas) {
		canvas.drawText(getString(R.string.app_name), canvas.getWidth() / 2,
				iPanelBegin / 2, paintForeground);
	}

	public void DrawTimes(Canvas canvas, int iPercent) {
		if (iPercent < 0)
			iPercent = 0;
		if (iPercent > 100)
			iPercent = 100;

		int iLengh = (canvas.getWidth() * 8 / 10);
		int iBeginX = (canvas.getWidth() - iLengh) / 2;

		Rect rectRound = new Rect(iBeginX, (iPanelBegin * 3 / 4), iBeginX
				+ iLengh, (iPanelBegin * 3 / 4) + 10);
		Rect rect = new Rect(iBeginX + 2, (iPanelBegin * 3 / 4) + 2, iBeginX
				+ (iLengh * iPercent / 100) - 1, (iPanelBegin * 3 / 4) + 10 - 1);
		canvas.drawRect(rectRound, paintTimesRound);
		canvas.drawRect(rect, paintTimes);
	}

	private void DrawPanel(Canvas canvas) {
		Paint paintBackGround = new Paint();
		paintBackGround.setColor(getResources().getColor(
				R.color.puzzle_background));
		canvas.drawRect(0, iPanelBegin, iPanelWidth,
				iPanelBegin + iPanelHeight, paintBackGround);
		Paint paintDark = new Paint();
		paintDark.setColor(getResources().getColor(R.color.puzzle_dark));

		Paint paintHilite = new Paint();
		paintHilite.setColor(getResources().getColor(R.color.puzzle_hilite));

		Paint paintLight = new Paint();
		paintLight.setColor(getResources().getColor(R.color.puzzle_light));

		for (int i = 0; i < 9; i++) {
			if (i % 3 == 0) {
				canvas.drawLine(0, iPanelBegin + i * fHeight, iPanelWidth,
						iPanelBegin + i * fHeight, paintDark);
				canvas.drawLine(i * fWidth, iPanelBegin, i * fWidth,
						iPanelBegin + iPanelHeight, paintDark);
			} else {
				canvas.drawLine(0, iPanelBegin + i * fHeight, iPanelWidth,
						iPanelBegin + i * fHeight, paintLight);
				canvas.drawLine(i * fWidth + 1, iPanelBegin, i * fWidth,
						iPanelBegin + iPanelHeight, paintLight);
			}

			canvas.drawLine(0, iPanelBegin + i * fHeight + 1, iPanelWidth,
					iPanelBegin + i * fHeight + 1, paintHilite);
			canvas.drawLine(i * fWidth + 1, iPanelBegin, i * fWidth + 1,
					iPanelBegin + iPanelHeight, paintHilite);

		}

	}

	public void DrawGame(Canvas canvas) {
		FontMetrics fm = paintForeground.getFontMetrics();

		for (int i = 0; i < 9; i++) {
			for (int j = 0; j < 9; j++) {
				if (mGame.IsLock(i, j)) {
					canvas.drawText(mGame.getStringTile(i, j), i * fWidth
							+ fWidth / 2, iPanelBegin + j * fHeight
							+ (fHeight - fm.ascent - fm.descent) / 2,
							paintForegroundLock);
				} else {
					canvas.drawText(mGame.getStringTile(i, j), i * fWidth
							+ fWidth / 2, iPanelBegin + j * fHeight
							+ (fHeight - fm.ascent - fm.descent) / 2,
							paintForeground);
				}
			}

		}
	}

	public void DrawSelected(Canvas canvas) {
		canvas.drawRect(recSel, paintSelected);
	}

	public void selected(int x, int y) {
		iSelX = Math.min(Math.max(x, 0), 8);
		iSelY = Math.min(Math.max(y, 0), 8);
		getRect(iSelX, iSelY, recSel);
	}

	private void getRect(int x, int y, Rect rect) {
		rect.set((int) (x * fWidth), (int) (iPanelBegin + y * fHeight),
				(int) (x * fWidth + fWidth),
				(int) (iPanelBegin + y * fHeight + fHeight));
	}

	public boolean setSelectedTile(int iTitle) {
		return mGame.setTileIfValid(iSelX, iSelY, iTitle);
	}

	public void ShowKeypad(int x, int y) {
		int tiles[] = mGame.getUsedtiles(x, y);
		System.out.println(tiles.length);
		if (tiles.length < 9 && !mGame.IsLock(x, y)) {
			Dialog dlgKeypad = new keypad(this, tiles, this);
			dlgKeypad.show();

		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_IGM_Resume:
			CloseDiglog();
			break;
		case R.id.btn_IGM_Restart:
		case R.id.btn_LoseRestart:
			mGame.Restart();
			CloseDiglog();
			break;
		case R.id.btn_IGM_mainmenu:
		case R.id.btn_Lose_MainMenu:
			GotoMainMenu();
			finish();
			break;
		case R.id.btn_WinOK:
			if (editText_WinName.getVisibility() == View.VISIBLE) {

			}
			GotoMainMenu();
			finish();
			break;

		default:
			break;
		}
	}

	private void GotoMainMenu() {
		Intent intentMainMenu = new Intent(this, mainMenu.class);
		startActivity(intentMainMenu);
	}

	private void CloseDiglog() {
		dlgWin.dismiss();
		dlgLose.dismiss();
		dlgInGameMenu.dismiss();
	}

	private boolean DiglogisShowing() {
		return (dlgWin.isShowing() || dlgLose.isShowing()
				| dlgInGameMenu.isShowing());
	}

	private void ShowGameDialog(int dlg) {
		if (dlg == GAME_MENU) {
			dlgWin.dismiss();
			dlgLose.dismiss();
			dlgInGameMenu.show();
		} else if (dlg == GAME_LOSE) {
			dlgWin.dismiss();
			dlgInGameMenu.dismiss();
			dlgLose.show();
		} else if (dlg == GAME_WIN) {
			dlgLose.dismiss();
			dlgInGameMenu.dismiss();

			if (false) {

			} else {
			}

			dlgWin.show();

		}
	}

}
